var ACTION_ADD = 0;

function ModifyMembership(oInputGrid, oRefreshGrid, aTargetIds, action, oTypeToAdd, oParentId)
{
if (oInputGrid)
{

var aTargetIds = getSelected(oInputGrid);
if (IsNull(aTargetIds) || aTargetIds.length == 0)
{
alert(LOCID_ACTION_NOITEMSELECTED);
return false;
}
}

var targetids = "";
var memberXml = "";
var lookupType = "BasicOwner";
var lookupSelectionStatus = "multi";

if (oTypeToAdd == Territory)
{
lookupSelectionStatus = "single";
}

if (oParentId)
{
lookupType = "TeamMembers";
}

for (i=0; i<aTargetIds.length; i++)
{
targetids += aTargetIds[i] + ";";
}

var retval = LookupObjects(null, lookupSelectionStatus, lookupType, ((oTypeToAdd==AppSalesPerson) ? SystemUser : oTypeToAdd), 0, null, oParentId);
var retvals = "";

if (!retval || retval.items.length == 0)
{

return false;
}
else
{
for (i=0; i<retval.items.length; i++)
{
retvals += retval.items[i].id + ";";
}



targetids = targetids.substr(0, targetids.length-1);
retvals = retvals.substr(0, retvals.length-1);

switch (oTypeToAdd)
{
case SystemUser:
case AppSalesPerson:
memberXml = "<membership><users>" + retvals + "</users><teams>" + targetids + "</teams></membership>";
break;

case Team:
memberXml = "<membership><users>" + targetids + "</users><teams>" + retvals + "</teams></membership>";
oTypeToAdd = SystemUser;
break;

case Territory:
memberXml = "<membership><users>" + targetids + "</users><teams>" + retvals + "</teams></membership>";
oTypeToAdd = AppSalesPerson;
break;
}

var oXml = CreateXmlDocument(false);
oXml.loadXML(memberXml);

var submitUrl = "";

if (action == ACTION_ADD)
{
submitUrl = prependOrgName("/_grid/cmds/cmd_adduserteam.aspx");
}

var xmlhttp = CreateXmlHttp();
xmlhttp.open("POST", submitUrl + "?iObjType=" + oTypeToAdd, false);
SetTokenInHeader(xmlhttp, submitUrl);
xmlhttp.send(oXml);





var oXml = CreateXmlDocument(false);
oXml.loadXML(xmlhttp.responseXML.xml);

if (oRefreshGrid && handleXMLErr(oXml))
{
document.all[oRefreshGrid].Refresh();
}
}
}



function processLookupIds( lookup_results )
{
try
{
var oLUItems = lookup_results.items;
var oIds	 = new Array(oLUItems.length);



if (oLUItems.length < 1)
{
butBegin.disabled = true;
return;
}



for (i=0; i<oLUItems.length; ++i)
{


oIds[i] = oLUItems[i].id;
}



butBegin.disabled = false;

__dialogXml = "<userids>" + oIds.join(";") + "</userids>";
}
catch(e)
{



butBegin.disabled = (__dialogXml == "");
}
}
